//package com.fuyingedu.speaker.manager;
//
//import com.alibaba.dashscope.audio.tts.SpeechSynthesisAudioFormat;
//import com.alibaba.dashscope.audio.tts.SpeechSynthesisParam;
//import com.alibaba.dashscope.audio.tts.SpeechSynthesisResult;
//import com.alibaba.dashscope.audio.tts.SpeechSynthesizer;
//import com.alibaba.dashscope.common.ResultCallback;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.pool2.impl.GenericObjectPool;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.util.concurrent.CountDownLatch;
//
//@Component
//@Slf4j
//public class SpeechSynthesizerManager {
//
//    @Autowired
//    @Qualifier("speechSynthesizerGenericObjectPool")
//    private GenericObjectPool<SpeechSynthesizer> speechSynthesizerGenericObjectPool;
//    @Value("${ali.dashscope.api-key}")
//    private String apiKey;
//
//    public void synthesizer(long timestamp, String text) throws InterruptedException {
//        CountDownLatch latch = new CountDownLatch(1);
//        SpeechSynthesizer speechSynthesizer = null;
//        try {
//            speechSynthesizer = speechSynthesizerGenericObjectPool.borrowObject();
//            SpeechSynthesizer synthesizer = speechSynthesizer;
//            SpeechSynthesisParam param = SpeechSynthesisParam.builder()
//                    .model("sambert-zhiwei-v1")
//                    .format(SpeechSynthesisAudioFormat.MP3)
//                    .text(text)
//                    .apiKey(apiKey)
//                    .build();
//            ResultCallback<SpeechSynthesisResult> callback = new ResultCallback<>() {
//                @Override
//                public void onEvent(SpeechSynthesisResult message) {
//                    log.info("Result: event");
//                    if (message.getAudioFrame() != null) {
//                        byte[] bytes = message.getAudioFrame().array();
//                        try (FileOutputStream fos = new FileOutputStream(String.format("C:\\workspace\\fuying-speaker\\target\\classes\\static\\%d.mp3", timestamp), true)) {
//                            fos.write(bytes);
//                        } catch (IOException e) {
//                            throw new RuntimeException(e);
//                        }
//                    }
//                }
//
//                @Override
//                public void onComplete() {
//                    log.info("Recognition complete");
//                    speechSynthesizerGenericObjectPool.returnObject(synthesizer);
//                    latch.countDown();
//                }
//
//                @Override
//                public void onError(Exception e) {
//                    closeRecognition(synthesizer);
//                }
//            };
//            synthesizer.call(param, callback);
//        } catch (Exception e) {
//            log.error("chat error", e);
//            if (speechSynthesizer != null) {
//                closeRecognition(speechSynthesizer);
//            }
//        }
//        latch.await();
//    }
//
//    private void closeRecognition(SpeechSynthesizer synthesizer) {
//        synthesizer.getSyncApi().close(1000, "bye");
//        try {
//            speechSynthesizerGenericObjectPool.invalidateObject(synthesizer);
//            speechSynthesizerGenericObjectPool.addObject();
//        } catch (Exception ignore) {}
//    }
//}
